Code
# install.packages("tidyverse")
# install.packages("ggplot2")
# install.packages("viridis")
library("tidyverse")
library("ggplot2")
library("viridis")
cohort.data <- data.frame(cohort=c('Cohort01', 'Cohort02', 'Cohort03', 'Cohort04', 'Cohort05', 'Cohort06', 'Cohort07', 'Cohort08', 'Cohort09', 'Cohort10', 'Cohort11', 'Cohort12'),
M1=c(800,0,0,0,0,0,0,0,0,0,0,0),
M2=c(250,850,0,0,0,0,0,0,0,0,0,0),
M3=c(240,230,652,0,0,0,0,0,0,0,0,0),
M4=c(235,225,230,823,0,0,0,0,0,0,0,0),
M5=c(232,223,180,293,655,0,0,0,0,0,0,0),
M6=c(215,215,180,290,300,736,0,0,0,0,0,0),
M7=c(150,215,170,221,267,291,700,0,0,0,0,0),
M8=c(150,200,159,215,266,287,315,854,0,0,0,0),
M9=c(140,190,150,200,249,255,287,320,823,0,0,0),
M10=c(135,150,142,189,240,250,285,295,296,820,0,0),
M11=c(128,145,138,170,215,245,265,275,280,290,700,0),
M12=c(120,130,135,154,210,205,198,275,280,285,300,896))
data <- cohort.data %>% pivot_longer( cols = -c(cohort), names_to = "month", values_to = "following") %>% mutate(month = factor(month, levels = c("M1",
"M2",
"M3",
"M4",
"M5",
"M6",
"M7",
"M8",
"M9",
"M10",
"M11",
"M12")))
#plot data
p <- ggplot(data, aes(x=month, y=following, group=cohort))
p + geom_area(aes(fill = cohort)) +
ggtitle('Number of persons who followed in cohort during 1 year') +
scale_color_viridis(option = "D") +
scale_fill_viridis(discrete = TRUE) +
theme_bw()
